package com.transactional;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Component
public class UserService {
	@Transactional
	public void test() {
		TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
			@Override
			public int getOrder() {
				return TransactionSynchronization.super.getOrder();
			}

			@Override
			public void suspend() {
				TransactionSynchronization.super.suspend();
			}

			@Override
			public void resume() {
				TransactionSynchronization.super.resume();
			}

			@Override
			public void flush() {
				TransactionSynchronization.super.flush();
			}

			@Override
			public void beforeCommit(boolean readOnly) {
				TransactionSynchronization.super.beforeCommit(readOnly);
			}

			@Override
			public void beforeCompletion() {
				TransactionSynchronization.super.beforeCompletion();
			}

			@Override
			public void afterCommit() {
				TransactionSynchronization.super.afterCommit();
			}

			@Override
			public void afterCompletion(int status) {
				TransactionSynchronization.super.afterCompletion(status);
			}
		});

		//Todo 执行数据库操作
		try {
			throw new NullPointerException();
		} catch (NullPointerException e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}

	}
}
